package cache

import (
	"fmt"
	"runtime"
)

// todo 结构体，数组，切片，map呢，长度应该怎么算？
// 数组，切片，map类型太多太多了，没办法全部写出来，应该是每定义一个类型，就让那个类型实现一个 Len 方法，这样就能得出长度
func CalcLen(value interface{}) int {
	var n int
	switch v := value.(type) {
	case Value: // 结构体，数组，切片，map,要求实现 Value 接口，该接口只有1个 Len 方法，返回占用的内存字节数，如果没有实现该接口，则panic
		n = v.Len()
	case string:
		if runtime.GOARCH == "amd64" {
			n = 16 + len(v)
		} else {
			n = 8 + len(v)
		}
	case bool, int8, uint8:
		n = 1
	case int16, uint16:
		n = 2
	case int32, uint32, float32:
		n = 4
	case int64, uint64, float64:
		n = 8
	case int, uint:
		if runtime.GOARCH == "amd64" {
			n = 8
		} else {
			n = 4
		}
	case complex64:
		n = 8
	case complex128:
		n = 16
	default:
		panic(fmt.Sprintf("%T is not implement cache.value", value))
	}

	return n
}
